// permission控制页面登陆权限的文件
import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist
router.beforeEach(async(to, from, next) => {
// 开启进度条
  NProgress.start()

  // set page title
  // document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()
  // 判断是否有token
  if (hasToken) {
    if (to.path === '/login') {
      // 如果有token且是进入登录页
      next({ path: '/' })
      // 强制跳转到主页
      NProgress.done()
      // 关闭进度条
    } else {
      // 路径是否在白名单内
      if (whiteList.indexOf(to.path) > -1) {
        next()
        // 放行
      } else {
        next('/login')
        // 强制跳转登录页
        NProgress.done()
      }
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
